#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int n, m;
    bool vis[200][200] = { 0 };
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int solve(vector<vector<char> >& grid) {
        n = grid.size(), m = grid[0].size();
        int count = 0;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == '1' && !vis[i][j])
                {
                    count++;
                    dfs(grid, i, j);
                }

            }
        }
        return count;
    }

    void dfs(vector<vector<char>>& grid, int x, int y)
    {
        vis[x][y] = true;
        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < n && b >= 0 && b < m && grid[a][b] == '1' && !vis[a][b])
            {
                dfs(grid, a, b);
            }
        }
    }
};